package com.itboye.lottery.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.bylibrary.widget.glide.GlideUtil;
import com.itboye.lottery.R;
import com.itboye.lottery.bean.PeopleBean;

import java.util.ArrayList;

/**
 * Created by wu on 2018/1/9.
 */

public class BangDanAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    ArrayList<PeopleBean> classEntityArrayList = new ArrayList<>();
    Activity activity;

    public BangDanAdapter(Activity activity, ArrayList<PeopleBean> classEntityArrayList) {
        this.classEntityArrayList = classEntityArrayList;
        this.activity = activity;
    }

    public OnItemClickListener itemClickListener;

    public void setOnItemClickListener(BangDanAdapter.OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new ViewHolderBangDan(LayoutInflater.from(context).inflate(R.layout.item_list_concern, parent, false));
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolderBangDan viewHolderBangDan = (ViewHolderBangDan) holder;
        PeopleBean peopleBean = classEntityArrayList.get(position);
        viewHolderBangDan.tv_username.setText(peopleBean.getNickname());
        viewHolderBangDan.tv_bet_history.setText(peopleBean.getNumber() + "连中");
        if (Integer.valueOf(peopleBean.getNumber()) > 0) {
            viewHolderBangDan.tv_bet_history.setVisibility(View.VISIBLE);
        } else {
            viewHolderBangDan.tv_bet_history.setVisibility(View.GONE);
        }
        viewHolderBangDan.tv_huibao.setText(Html.fromHtml("单位回报:<font color='red'>" + String.format("%.2f", peopleBean.getRate_return()) + "</font>%"));
        viewHolderBangDan.tv_bet_total_bonus.setText(Html.fromHtml("总奖金:<font color='red'>" + String.format("%.2f", Double.parseDouble(peopleBean.getPlan_amount())) + "</font>元"));
        if (peopleBean.getPlan_count() > 0) {
            viewHolderBangDan.tv_follow_number.setText(peopleBean.getPlan_count() + "");
            viewHolderBangDan.tv_is_follow.setBackgroundResource(R.drawable.rounded_bg_red_border_none);
            viewHolderBangDan.tv_is_follow.setEnabled(true);
            viewHolderBangDan.tv_is_follow.setText("可跟单");
            viewHolderBangDan.tv_follow_number.setVisibility(View.VISIBLE);
//            viewHolderBangDan.tv_is_follow.setOnClickListener((BangDanActivity) activity);
        } else {
            viewHolderBangDan.tv_is_follow.setBackgroundResource(R.drawable.rounded_bg_gray_border_none);
            viewHolderBangDan.tv_is_follow.setEnabled(false);
            viewHolderBangDan.tv_is_follow.setText("不可跟单");
            viewHolderBangDan.tv_follow_number.setVisibility(View.GONE);
        }
        viewHolderBangDan.iv_user_rank.setVisibility(View.GONE);
        viewHolderBangDan.iv_user_rank_2.setVisibility(View.GONE);
        viewHolderBangDan.tv_user_number.setText("" + (position + 1));
        GlideUtil.displayForCircle(context, peopleBean.getHead_pic(), R.mipmap.head_default, viewHolderBangDan.iv_user_icon);
        switch (position) {
            case 0:
                viewHolderBangDan.iv_user_rank.setImageResource(R.mipmap.guanjun_1);
                viewHolderBangDan.iv_user_rank_2.setImageResource(R.mipmap.guanjun_2);
                viewHolderBangDan.iv_user_rank.setVisibility(View.VISIBLE);
                viewHolderBangDan.iv_user_rank_2.setVisibility(View.VISIBLE);
                viewHolderBangDan.tv_user_number.setTextColor(Color.parseColor("#F8E71C"));
                break;
            case 1:
                viewHolderBangDan.iv_user_rank.setImageResource(R.mipmap.yajun_1);
                viewHolderBangDan.iv_user_rank_2.setImageResource(R.mipmap.yajun_2);
                viewHolderBangDan.iv_user_rank.setVisibility(View.VISIBLE);
                viewHolderBangDan.iv_user_rank_2.setVisibility(View.VISIBLE);
                viewHolderBangDan.tv_user_number.setTextColor(Color.parseColor("#C6C6C6"));
                break;
            case 2:
                viewHolderBangDan.iv_user_rank.setImageResource(R.mipmap.jijun_1);
                viewHolderBangDan.iv_user_rank_2.setImageResource(R.mipmap.jijun_2);
                viewHolderBangDan.iv_user_rank.setVisibility(View.VISIBLE);
                viewHolderBangDan.iv_user_rank_2.setVisibility(View.VISIBLE);
                viewHolderBangDan.tv_user_number.setTextColor(Color.parseColor("#C48E22"));
                break;
        }
    }

    @Override
    public int getItemCount() {
        return classEntityArrayList == null ? 0 : classEntityArrayList.size();
    }


    public class ViewHolderBangDan extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView tv_user_number, tv_username, tv_bet_history, tv_huibao, tv_bet_total_bonus, tv_is_follow, tv_follow_number;
        ImageView iv_user_icon, iv_user_rank, iv_user_rank_2;
        public RelativeLayout item_rl_chongzhi_jine;

        public ViewHolderBangDan(View itemView) {
            super(itemView);
            item_rl_chongzhi_jine = itemView.findViewById(R.id.item_rl_chongzhi_jine);
            tv_user_number = itemView.findViewById(R.id.tv_user_number);
            iv_user_icon = itemView.findViewById(R.id.iv_user_icon);
            iv_user_rank = itemView.findViewById(R.id.iv_user_rank);
            iv_user_rank_2 = itemView.findViewById(R.id.iv_user_rank_2);
            tv_username = itemView.findViewById(R.id.tv_username);
            tv_bet_history = itemView.findViewById(R.id.tv_bet_history);
            tv_huibao = itemView.findViewById(R.id.tv_huibao);
            tv_bet_total_bonus = itemView.findViewById(R.id.tv_bet_total_bonus);
            tv_is_follow = itemView.findViewById(R.id.tv_is_follow);
            tv_follow_number = itemView.findViewById(R.id.tv_follow_number);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }

}
